<?php
/**
 * Magento Webshopapps Order Export Module
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category   Webshopapps
 * @package    Webshopapps_OrderExport
 * @copyright  Copyright (c) 2010 Zowta Ltd (http://www.webshopapps.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Genevieve Eddison <sales@webshopapps.com>
 * */
class Webshopapps_Ordermanager_Model_Export_Csv extends Webshopapps_Ordermanager_Model_Export_Abstractcsv
{
    const ENCLOSURE = '"';
    const DELIMITER = ',';

    /**
     * Concrete implementation of abstract method to export given orders to csv file in var/export.
     *
     * @param $orders List of orders of type Mage_Sales_Model_Order or order ids to export.
     * @return String The name of the written csv file in var/export
     */
    public function exportOrders($orders) 
    {
        $fileName = 'order_export_'.date("Ymd_His").'.csv';
        $fp = fopen(Mage::getBaseDir('export').'/'.$fileName, 'w');
        
        $this->writeHeadRow($fp);
        foreach ($orders as $order) {
        	$order = Mage::getModel('sales/order')->load($order);
            $this->writeOrder($order, $fp);
        }
		
        fclose($fp);
        
        return $fileName;
    }

    /**
	 * Writes the head row with the column names in the csv file.
	 * 
	 * @param $fp The file handle of the csv file
	 */
    protected function writeHeadRow($fp) 
    {
        fputcsv($fp, $this->getHeadRowValues(), self::DELIMITER, self::ENCLOSURE);
    }

    /**
	 * Writes the row(s) for the given order in the csv file.
	 * A row is added to the csv file for each ordered item. 
	 * 
	 * @param Mage_Sales_Model_Order $order The order to write csv of
	 * @param $fp The file handle of the csv file
	 */
    protected function writeOrder($order, $fp) 
    {
        $common = $this->getCommonOrderValues($order);

        $orderItems = $order->getItemsCollection();
        $itemInc = 0;
        // foreach ($orderItems as $item)
        // {
            // if (!$item->isDummy()) {
                // $record = array_merge($common, $this->getOrderItemValues($item, $order, ++$itemInc));
                // fputcsv($fp, $record, self::DELIMITER, self::ENCLOSURE);
            // }
        // }
		$record = array_merge($common, $this->getOrderItemValues($item, $order, ++$itemInc));
        fputcsv($fp, $record, self::DELIMITER, self::ENCLOSURE);
    }

    /**
	 * Returns the head column names.
	 * 
	 * @return Array The array containing all column names
	 */
    protected function getHeadRowValues() 
    {
        return array(
            'Order Number',
			'Merchant Order ID',
			'eNets Transaction ID',
			'Authorisation Code',
            'Purchased On',
            
            //'Order Purchased From',
            // 'Order Payment Method',
            // 'Order Shipping Method',
            //'Order Subtotal',
            //'Order Tax',
            //'Order Shipping',
            //'Order Discount',
            
        	// 'Order Paid',
            // 'Order Refunded',
            // 'Order Due',
            // 'Total Qty Items Ordered',
            // 'Customer Name',
            
			// 'Customer Email',
            'Billing Name',
			'Shipping Name',
            // 'Shipping Company',
            // 'Shipping Street',
            // 'Shipping Zip',
            // 'Shipping City',
        	// 'Shipping State',
            // 'Shipping State Name',
            // 'Shipping Country',
            // 'Shipping Country Name',
            // 'Shipping Phone Number',
    		
            'G.T. (Base)',
			'G.T. (Purchased)',            
			'Order Status',
			// 'Billing Company',
            // 'Billing Street',
            // 'Billing Zip',
            // 'Billing City',
        	// 'Billing State',
            // 'Billing State Name',
            // 'Billing Country',
            // 'Billing Country Name',
            // 'Billing Phone Number',
            // 'Order Item Increment',
    		// 'Item Name',
            // 'Item Status',
            // 'Item SKU',
            // 'Item Options',
            // 'Item Original Price',
    		// 'Item Price',
            // 'Item Qty Ordered',
        	// 'Item Qty Invoiced',
        	// 'Item Qty Shipped',
        	// 'Item Qty Canceled',
            // 'Item Qty Refunded',
            // 'Item Tax',
            // 'Item Discount',
    		// 'Item Total'
    	);
    }

    /**
	 * Returns the values which are identical for each row of the given order. These are
	 * all the values which are not item specific: order data, shipping address, billing
	 * address and order totals.
	 * 
	 * @param Mage_Sales_Model_Order $order The order to get values from
	 * @return Array The array containing the non item specific values
	 */
    protected function getCommonOrderValues($order) 
    {
        $shippingAddress = !$order->getIsVirtual() ? $order->getShippingAddress() : null;
        $billingAddress = $order->getBillingAddress();
		$resource = Mage::getSingleton('core/resource');
		$readConnection = $resource->getConnection('core_read');
		$transquery = 'SELECT nets_txn_ref FROM enets_transaction WHERE (merchant_txn_ref = "'. $order->getRealOrderId() . '")  LIMIT 1';		
		$trans = $readConnection->fetchOne($transquery);
		
		$midquery = 'SELECT merchant_txn_ref FROM enets_transaction WHERE (merchant_txn_ref = "'. $order->getRealOrderId() . '")  LIMIT 1';		
		$mid = $readConnection->fetchOne($midquery);
		
		$codequery = 'SELECT bank_auth_id FROM enets_transaction WHERE (merchant_txn_ref = "'. $order->getRealOrderId() . '")  LIMIT 1';
		$code = $readConnection->fetchOne($codequery);
		
		$codequery1 = 'SELECT payment_mode FROM enets_transaction WHERE (merchant_txn_ref = "'. $order->getRealOrderId() . '")  LIMIT 1';
		$codequery2 = 'SELECT bank_ref_code FROM enets_transaction WHERE (merchant_txn_ref = "'. $order->getRealOrderId() . '")  LIMIT 1';
		$pay = $readConnection->fetchOne($codequery1);
		if($pay == "DD"){
			$code = $readConnection->fetchOne($codequery2);
		}
        return array(
            $order->getRealOrderId(),
			$mid,
			$trans,
			$code,
            Mage::helper('core')->formatDate($order->getCreatedAt(), 'medium', true),
            
            // $this->getStoreName($order),
            // $this->getPaymentMethod($order),
            // $this->getShippingMethod($order),
            // $this->formatPrice($order->getData('subtotal'), $order),
            // $this->formatPrice($order->getData('tax_amount'), $order),
            // $this->formatPrice($order->getData('shipping_amount'), $order),
            // $this->formatPrice($order->getData('discount_amount'), $order),
            
            // $this->formatPrice($order->getData('total_paid'), $order),
            // $this->formatPrice($order->getData('total_refunded'), $order),
            // $this->formatPrice($order->getData('total_due'), $order),
            // $this->getTotalQtyItemsOrdered($order),
            // $order->getCustomerName(),
            // $order->getCustomerEmail(),
            $billingAddress->getName(),
			$shippingAddress ? $shippingAddress->getName() : '',
            // $shippingAddress ? $shippingAddress->getData("company") : '',
            // $shippingAddress ? $shippingAddress->getData("street") : '',
            // $shippingAddress ? $shippingAddress->getData("postcode") : '',
            // $shippingAddress ? $shippingAddress->getData("city") : '',
            // $shippingAddress ? $shippingAddress->getRegionCode() : '',
            // $shippingAddress ? $shippingAddress->getRegion() : '',
            // $shippingAddress ? $shippingAddress->getCountry() : '',
            // $shippingAddress ? $shippingAddress->getCountryModel()->getName() : '',
            // $shippingAddress ? $shippingAddress->getData("telephone") : '',
            
			$this->formatPrice($order->getData('base_grand_total'), $order),
			$this->formatPrice($order->getData('grand_total'), $order),
			$order->getStatus(),
            // $billingAddress->getData("company"),
            // $billingAddress->getData("street"),
            // $billingAddress->getData("postcode"),
            // $billingAddress->getData("city"),
            // $billingAddress->getRegionCode(),
            // $billingAddress->getRegion(),
            // $billingAddress->getCountry(),
            // $billingAddress->getCountryModel()->getName(),
            // $billingAddress->getData("telephone")
        );
    }

    /**
	 * Returns the item specific values.
	 * 
	 * @param Mage_Sales_Model_Order_Item $item The item to get values from
	 * @param Mage_Sales_Model_Order $order The order the item belongs to
	 * @return Array The array containing the item specific values
	 */
    protected function getOrderItemValues($item, $order, $itemInc=1) 
    {
    	
        return array(
            // $itemInc,
            // $item->getName(),
            // $item->getStatus(),
            // $this->getItemSku($item),
            // $this->getItemOptions($item),
            // $this->formatPrice($item->getOriginalPrice(), $order),
            // $this->formatPrice($item->getData('price'), $order),
            // (int)$item->getQtyOrdered(),
            // (int)$item->getQtyInvoiced(),
            // (int)$item->getQtyShipped(),
            // (int)$item->getQtyCanceled(),
        	// (int)$item->getQtyRefunded(),
            // $this->formatPrice($item->getTaxAmount(), $order),
            // $this->formatPrice($item->getDiscountAmount(), $order),
            // $this->formatPrice($this->getItemTotal($item), $order)
        );
    }
}
?>